package com.gin.kotlin.gf2gamecommunity.composable

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.Face
import androidx.compose.material3.Button
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.base.composable.MyImage
import com.gin.kotlin.gf2gamecommunity.callback.AbstractNothingCallback
import com.gin.kotlin.gf2gamecommunity.routes.EmojiDialog
import com.gin.kotlin.gf2gamecommunity.routes.ReplyRoute

@Composable
fun ReplyFAB(route: ReplyRoute, onNavigateToReply: (ReplyRoute) -> Unit, onRefresh: () -> Unit = {}) {
    var showEmojiDialog by remember { mutableStateOf(false) }
    var selectedEmojiUrl by remember { mutableStateOf<String?>(null) }
    var selectedHtml by remember { mutableStateOf("") }

    Row(horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.background(MaterialTheme.colorScheme.background.copy(alpha = 0.8f), RoundedCornerShape(16.dp))) {
        IconButton(onClick = { showEmojiDialog = true }) {
            Icon(imageVector = Icons.Filled.Face, contentDescription = "纯表回复")
        }
        IconButton(onClick = { onNavigateToReply(route) }) {
            Icon(imageVector = Icons.Filled.Create, contentDescription = "回复")
        }
    }

    if (showEmojiDialog) EmojiDialog(title = "纯表回复",
        onDismissRequest = { showEmojiDialog = false },
        onClickEmoji = { e, html ->
            selectedEmojiUrl = e.urlPath
            selectedHtml = html
        }) {
        HorizontalDivider(modifier = Modifier.height(8.dp))

        Row(horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.fillMaxWidth()) {
            Row {
                Text("选中：")
                MyImage(selectedEmojiUrl, modifier = Modifier.size(80.dp))
            }

            Button(onClick = {
                App.INSTANCE.api.commentApi.reply(route.toForm(selectedHtml)).enqueue(object : AbstractNothingCallback() {
                    override fun onSuccess(message: String) {
                        // App.INSTANCE.replyFlag = true
                        // 回复成功退出界面
                        App.toast(message)
                        showEmojiDialog = false
                        onRefresh()
                    }
                })
            }) { Text("确认") }
        }
    }
}